#include "main.h"
#include "my_main.h"
#include <stdio.h>
#include "usbd_cdc_if.h"


extern DMA_HandleTypeDef hdma_usart2_rx;

uint8_t rev[10];

int _write(int file, char *ptr, int len)
{
    HAL_StatusTypeDef status = HAL_OK;

    if (file == 1 || file == 2)
    {
        status = HAL_UART_Transmit_DMA(&huart2, (uint8_t*)ptr, len);
        if (status != HAL_OK)
        {
            return -1;
        }
        return len;
    }
    else
    {
        return -1;
    }
}

void out_while(void){
	HAL_UARTEx_ReceiveToIdle_DMA(&huart2, rev, sizeof(rev));
	__HAL_DMA_DISABLE_IT(&hdma_usart2_rx, DMA_IT_HT);


}

int in_while(void){
	//USB模拟串口的查寻接收处理（其编程原理与USART1串口收发相同）(%。*s的转译符用法可自行百度）
	if(USB_RX_STA!=0)//判断是否有数据
	{
		USB_printf("USB_RX:%.*s\r\n",USB_RX_STA,USB_RX_BUF);//向USB模拟串口发送字符串
		USB_RX_STA=0;//数据标志位清0
		memset(USB_RX_BUF,0,sizeof(USB_RX_BUF));//USB串口数据寄存器清0
	}
    HAL_Delay(1500);//毫秒延时
    return 0;
}

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
  if(GPIO_Pin == GPIO_PIN_4){
	  HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_4));
  }

}


//void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
//{
//	  HAL_UART_Transmit_DMA(&huart2, (uint8_t *)rev, sizeof(rev));
//	  HAL_UARTEx_ReceiveToIdle_DMA(&huart2, rev, 2);
//}

//void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
//{
//	return;
//}

void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size){
	if(huart == &huart2){
		// do something about rev
		 HAL_UARTEx_ReceiveToIdle_DMA(&huart2, rev, sizeof(rev));
		 __HAL_DMA_DISABLE_IT(&hdma_usart2_rx, DMA_IT_HT);
	}
}


